//取消预定的主函数
//主要作用是删除全局变量里面用户预定的订单并将其添加到取消订单内
function cancelBook(customerList, customerCancel) {
    let space = customerCancel.space;
    let user = customerCancel.user;
    let time = customerCancel.time;
    let date = customerCancel.date;
    if (customerList.hasOwnProperty(space) && customerList[space].hasOwnProperty(date) && customerList[space][date].hasOwnProperty(user) && customerList[space][date][user].indexOf(time) !== -1) {
        if (customerList[space][date].hasOwnProperty("cancelled")) {
            customerList[space][date].cancelled.push(time);
        } else {
            customerList[space][date].cancelled = [time];
        }
        let usedIndex = customerList[space][date].usedTime.indexOf(time);
        customerList[space][date].usedTime.splice(usedIndex, 1);
        if (customerList[space][date][user].length > 1) {
            let timeIndex = customerList[space][date][user].indexOf(time);
            customerList[space][date][user].splice(timeIndex, 1);
            return customerList;
        } else {
            delete customerList[space][date][user];
            return customerList;
        }
    } else {
        return "Error: the booking being cancelled does not exist";
    }
}

module.exports.cancelBook = cancelBook;